import QtQuick 1.0
//import ShellInvoker 1.0
import "../../../common/lib/kdeadmouseutils-build-desktop"

FocusScope {
    id: mainItem
    width: 1200
    height: 500
    focus:true
    anchors.centerIn: parent
    Rectangle{
      anchors.fill : parent
      color : '#3E606F'
    }
    signal showDetail()
    
    function getModelSelected () {
      var index = lPosterId.currentIndex;
      return movieBuildModel.get(index);
    }
    
    ListModel {
	  id : movieBuildModel
    }
    
    XmlListModel {
	property string movieIdAdding
	property variant itemAdding
	property int numTrait : 0
	id: movieXmlModel
	source: 'http://api.allocine.fr/xml/movie?code=' + movieIdAdding + '&partner=3&profile=medium'
	query: "/movie"
	namespaceDeclarations: "declare default element namespace 'http://www.allocine.net/v6/ns/';"
	XmlRole { name: "id" ; query: "@code/string()" ; isKey : true}
	XmlRole { name: "title"; query: "title/string()" }
	XmlRole { name: "productionYear"; query: "productionYear/string()" }
	XmlRole { name: "duree"; query: "runtime/string()" }
	XmlRole { name: "synopsisShort"; query: "synopsisShort/string()" }
	XmlRole { name: "directors"; query: "castingShort/directors/string()" }
	XmlRole { name: "actors"; query: "castingShort/actors/string()" }
	XmlRole { name: "poster"; query: "poster/@href/string()" }
	XmlRole { name: "trailerCode"; query: "trailer/@code/string()" }
	
	onStatusChanged: {
	      if (status == XmlListModel.Ready){
		  //pour une raison que j ignore, le reload ne marche pas !!!
		  if(count==1){
		    var obj = get(0);
		    obj.url=itemAdding.url;
		    movieBuildModel.append(obj);
		  }
		  addMovie();
	      }
	}
	
	function addMovie(){
	  var item = magikModelId.get(numTrait);
	  if(!item){
	    return;
	  }
	  numTrait++;
	  if(item.numPoster==-1){
	    return addMovie();
	  }
	  movieIdAdding=item.idAllocine;
	  itemAdding=item;
	  return;
	}
    }
    
    ListModel {
	  id : magikModelId
    }
    
    ShellInvoker {
	id:magikFileTube
	property int numPoster : 0
	waitFor:'start'
	onFinished : {
	  movieXmlModel.addMovie();
	}
	onOutputLineChanged : {
	    var sl = "("+ magikFileTube.outputLine +")" ;
	    try{
	      var l = eval(sl);
	      var idAllocine = l.idAllocine;
	      if(idAllocine){
		l.numPoster=numPoster;
		numPoster=numPoster+1;
	      }else{
		l.numPoster=-1;
	      }
	      magikModelId.append(l);
	    }catch(err){
	      console.log(err);
	      console.log("eval##"  + sl+"##");
	    }
	}
	
    }
   
    	Rectangle {
	      id:rectPosterId
	      height: parent.height
	      width:  parent.width - rectListNames.width
	      color:'transparent'
	      border.color:'green'
	      border.width:5
	      
	      anchors.verticalCenter:parent.verticalCenter
	      anchors.left: parent.left
	      //color: ''
	      ListView {
		      transform: Rotation { origin.x: 30; origin.y: 30; axis { x: 1; y: 1; z: 0 } angle: 25}
		      //transform: Rotation { origin.x: lPosterId.width; origin.y: 100; axis { x: 0; y: 1; z: 0 } angle: 45 }
		      id: lPosterId
		      anchors.fill : parent
		      model:movieBuildModel
		      orientation : ListView.Horizontal
		      Behavior on currentIndex { SpringAnimation { spring: 2; damping: 0.2 } }
		      
		      delegate: Item {
				      anchors.leftMargin: 10
				      anchors.rightMargin: 10
				      width: ListView.view.width
				      height: ListView.view.height
				      Image {
					    anchors.fill : parent
					    id:posterImg
					    fillMode: Image.PreserveAspectFit
					    source: poster
					    smooth: true
				      }
		      }
		      
		  }
	}
	
    Rectangle {
	  id:rectListNames
	  color:'transparent'
	  anchors.right: parent.right
	  height: parent.height
	  width : 500
	  anchors.verticalCenter:parent.verticalCenter
	  smooth: true
	  ListView {
		  id: lNamesId
		  width: parent.width - 40
		  height: parent.height - 40
		  anchors.centerIn: parent
		  model:magikModelId
		  orientation : ListView.Vertical
		  spacing : 20
		  focus: true
		  delegate: Rectangle {
				  id:rectItem
				  color: "#91AA9D"
				  smooth: true
				  radius: 5
				  anchors.horizontalCenter:parent.horizontalCenter
				  width: ListView.view.width - 100
				  height: 40
				  Text {
				      id:titleTxt
				      anchors.fill : parent
				      horizontalAlignment: Text.AlignLeft
				      text: title
				      color: "#193441"
				      font.pixelSize: 20
				    }
				   states: 
				   [
				   State {
					name: "active"; when: rectItem.activeFocus
					PropertyChanges { target: rectItem;scale: 1.3}
					PropertyChanges { target: lPosterId;currentIndex: numPoster}
					
					/*StateChangeScript {
					      name: "myScript"
					      script: lPosterId.positionViewAtIndex(numPoster,ListView.Center);
					}*/
				    }
				  ]
				    transitions: Transition {
					NumberAnimation { properties: "scale"; duration: 70 }
					//NumberAnimation { properties: "currentIndex"; duration: 20 }
				    }
				    
				    Keys.onReturnPressed : {
					console.log('item selected');
					mainItem.showDetail();
					event.accepted = true;
				    }
		  }
		  
	      }
    }
    

    
    
    
    
    
    Component.onCompleted: {
      //sed -ie 's/,$//' /tmp/magikFile.json
      magikFileTube.shellAction('cat',['/tmp/magikFile.json']);
    }  
   
        
}
 
